using Vin.Extension.Framework.Entities;
using Vin.Extension.JwtBearer.Tool;

namespace Vin.Extension.Framework.Extension;

public static class EntityExtension
{
    #region 设置创建信息

    public static void SetCreateInfo<T>(this T entity) where T : VinFrameworkBaseEntity, new()
    {
        var tokenModel = VinSecurityTool.GetTokenModelByToken();

        if (tokenModel != null)
        {
            entity.CreateUserId = tokenModel.RealUserId;
            entity.CreateUserName = tokenModel.UserName;
            entity.CreateAccountId = tokenModel.RealAccountId;
            entity.CreateAccountName = tokenModel.AccountName;
        }
    }

    public static void SetRecordCreateInfo<T>(this T entity) where T : VinFrameworkBaseRecordEntity, new()
    {
        var tokenModel = VinSecurityTool.GetTokenModelByToken();

        if (tokenModel != null)
        {
            entity.CreateUserId = tokenModel.RealUserId;
            entity.CreateUserName = tokenModel.UserName;
            entity.CreateAccountId = tokenModel.RealAccountId;
            entity.CreateAccountName = tokenModel.AccountName;
        }
    }

    #endregion

    #region 设置修改信息

    public static void SetUpdateInfo<T>(this T entity) where T : VinFrameworkBaseEntity, new()
    {
        var tokenModel = VinSecurityTool.GetTokenModelByToken();
        
        entity.UpdateTime = DateTime.Now;
        if (tokenModel != null)
        {
            entity.UpdateUserId = tokenModel.RealUserId;
            entity.UpdateUserName = tokenModel.UserName;
            entity.UpdateAccountId = tokenModel.RealAccountId;
            entity.UpdateAccountName = tokenModel.AccountName;
        }
    }

    public static void SetRecordUpdateInfo<T>(this T entity) where T : VinFrameworkBaseRecordEntity, new()
    {
        var tokenModel = VinSecurityTool.GetTokenModelByToken();
        
        entity.UpdateTime = DateTime.Now;
        if (tokenModel != null)
        {
            entity.UpdateUserId = tokenModel.RealUserId;
            entity.UpdateUserName = tokenModel.UserName;
            entity.UpdateAccountId = tokenModel.RealAccountId;
            entity.UpdateAccountName = tokenModel.AccountName;
        }
    }
    
    #endregion
}